package app.purchase.a571xz.com.myandroidframe.httpservice.retrofit.cookie;

import java.util.List;

import app.purchase.a571xz.com.myandroidframe.MyApplication;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

/**
 * Created by qianting on 2017/9/21.
 * 实例化CookieJar
 */

public class CookieJarImp implements CookieJar {
    //这里一定一定一定是HashMap<String, List<Cookie>>,是String,不是url.
    // private PersistentCookieStore cookieStore = new PersistentCookieStore(BaseApplication.getInstance().getApplicationContext());
    private static PersistentCookieStore cookieStore;

    public CookieJarImp() {
        if (cookieStore == null) {
            cookieStore = new PersistentCookieStore(MyApplication.getApplication());
        }
    }

    @Override
    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        if (cookies != null && cookies.size() > 0) {
            for (Cookie cookie : cookies) {
                cookieStore.add(url, cookie);
            }
        }
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl url) {
        List<Cookie> cookies = cookieStore.get(url);
        return cookies;
    }
}
